﻿using BookStorage.Service;
using Code.Repsitory.MenuRoles;
using Code.Repsitory.Roles;
using Code.Services.Roles.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Code.Services.Roles
{
    public class RoleService : IRoleService
    {
        private readonly IRoleRepository repository;
        private readonly IMenuRoleRepository menuRoleRepository;

        public RoleService(IRoleRepository repository, IMenuRoleRepository menuRoleRepository)
        {
            this.repository = repository;
            this.menuRoleRepository = menuRoleRepository;
        }

        public ResultDto<List<RoleDto>> QueryAll()
        {
            return new ResultDto<List<RoleDto>>
            {
                Data = repository.QueryAll().Select(m => new RoleDto
                {
                    CreateTime = m.CreateTime,
                    RoleId = m.RoleId,
                    RoleName = m.RoleName
                }).ToList(),
                Code = 200
            };
        }

        public ResultDto<List<int>> QueryPermission(int roleId)
        {
            return new ResultDto<List<int>>
            {
                Data = menuRoleRepository.Query(m => m.RoleId == roleId).Select(m => m.MenuId).ToList(),
                Code = 200
            };
        }
    }
}
